In this post I will explain you How to Generate PDF from Mysql Database using PHP. To Generate PDF file with mysql data content and output, we will use popular php library FPDF which will help us to generate PDF file.
What is FPDF ?
- FPDF is a PHP class library which allow us to generate PDF file using PHP.
- In FPDF, ‘F’ stands for Free. So We can say FPDF means Free PDF .
- It is Open Source library. You may use it and modify for any kind of usage to suit you needs.
Features or Advantages of FPDF Library:
- You can manage page format and margin.
- Easily Header and Footer Management of PDF file.
- Automatic Page Break
- Line break and text justification automatically.
- Image Support with format JPEG,JPG,PNG,GIF.
- Links, Colors, True Type and encoding Support.
- Page Compression
Step by Step Process to generate PDF using FPDF:
Step-1: Select the data from MySQL database into the page
Step-2: Download the FPDF library from fpdf.org
Step-3: Upload FPDF file into your application Folder
Step-4: Include the fpdf.php file into your application file
users.sql
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
-- -- Table structure for table `users` -- CREATE TABLE IF NOT EXISTS `users` ( `user_id` int(255) NOT NULL AUTO_INCREMENT, `name` varchar(1000) NOT NULL, `email` varchar(1000) NOT NULL, `department` varchar(1000) NOT NULL, `role` varchar(100) NOT NULL, `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Dumping data for table `users` -- INSERT INTO `users` (`user_id`, `name`, `email`, `department`, `role`, `created_on`) VALUES (5, 'pradeep', 'pradeep@gmail.com', 'Admin', 'admin', '2017-06-29 03:42:04'), (6, 'vikas', 'vikas@gmail.com', 'Web Development', 'employee', '2017-06-29 03:42:20'), (7, 'Suresh', 'suresh@gmail.com','Web Development', 'employee', '2017-07-05 18:27:24'), (8, 'Mukesh', 'mukesh@gmail.com', 'SEO', 'employee', '2017-07-05 18:27:44'), (9, 'Sandeep', 'sandeep@gmail.com', 'SEO', 'employee', '2017-07-05 18:28:03'); |
dbconfig.php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php session_start(); $host="localhost"; $username="root"; $pass=""; $db="tutorialswebsite"; $conn=mysqli_connect($host,$username,$pass,$db); if(!$conn){ die("Database connection error"); } ?> |
generate-user-pdf.php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php //include connection file include "dbconfig.php"; include_once('pdf/fpdf.php'); class PDF extends FPDF { // Page header function Header() { // Logo $this->Image('https://i2.wp.com/tutorialswebsite.com/wp-content/uploads/2016/01/cropped-LOGO-1.png',10,10,50); $this->SetFont('Arial','B',13); // Move to the right $this->Cell(80); // Title $this->Cell(80,10,'Employee List',1,0,'C'); // Line break $this->Ln(20); } // Page footer function Footer() { // Position at 1.5 cm from bottom $this->SetY(-15); // Arial italic 8 $this->SetFont('Arial','I',8); // Page number $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); } } $display_heading = array('user_id'=>'ID', 'name'=> 'Name', 'email'=> 'Email','department'=> 'Department','role'=> 'Role'); $result = mysqli_query($conn, "SELECT user_id, name, email, department, role FROM users") or die("database error:". mysqli_error($conn)); $header = mysqli_query($conn, "SHOW columns FROM users WHERE field != 'created_on'"); $pdf = new PDF(); //header $pdf->AddPage(); //foter page $pdf->AliasNbPages(); $pdf->SetFont('Arial','B',16); foreach($header as $heading) { $pdf->Cell(35,10,$display_heading[$heading['Field']],1); } foreach($result as $row) { $pdf->SetFont('Arial','',10); $pdf->Ln(); foreach($row as $column) $pdf->Cell(35,10,$column,1); } $pdf->Output(); ?> |
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co